﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;

namespace Utility
{
    public static class Utility
    {
        public static void WriteLine(System.Web.UI.Page page, string str)
        {
            if (str != string.Empty)
            {
                page.Response.Write("<br>");
                page.Response.Write(str);
                page.Response.Write("<br>");
            }
        }


        #region Hyperion


        public static double? GetDouble(object obj)
        {
            return GetDouble(obj, null);
        }
        public static double? GetDouble(object obj, double? defaultValue)
        {
            if (obj == null)
                return defaultValue;

            double result;

            if (double.TryParse(obj.ToString(), out result))
                return result;
            else
                return defaultValue;
        }

        public static double? GetFiledValue(DataTable dt, string strFiled)
        {
            return GetFiledValue(dt, strFiled, null);
        }

        public static double? GetFiledValue(DataTable dt, string strFiled, double? defaultValue)
        {
            if (dt == null)
                return defaultValue;

            DataRow[] Rows = dt.Select("name='" + strFiled + "'");

            if (Rows.Length < 1)
                return defaultValue;

            return GetDouble(Rows[0]["count1"], defaultValue);
        }

        #endregion

        #region Sshi

        public static int DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            int dateDiff = 0;
            try
            {
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                dateDiff = ts.Days;
            }
            catch
            {
            }
            return dateDiff < 0 ? 0 - dateDiff : dateDiff;
        }

        /*
        public static string formatPct(string val, int dec)
        {
            float n = 0;
            StringBuilder strFormat = new StringBuilder();
            strFormat.Append(@"{0:0");
            if (dec > 0)
            {
                strFormat.Append(".");
                for (int i = 0; i < dec; i++)
                {
                    strFormat.Append("0");
                }
            }
            strFormat.Append("%}");

            if (float.TryParse(val, out n))
            {
                float val1 = CbmUtils.ParseFloat(val, 0);
                return string.Format(strFormat.ToString(), val1 / 100);
            }
            else
            {
                return string.Format(strFormat.ToString(), 0 / 100);
            }
        }
        public static string formatPct(float val, int dec)
        {
            float n = 0;
            StringBuilder strFormat = new StringBuilder();
            strFormat.Append(@"{0:0");
            if (dec > 0)
            {
                strFormat.Append(".");
                for (int i = 0; i < dec; i++)
                {
                    strFormat.Append("0");
                }
            }
            strFormat.Append("%}");

            if (float.TryParse(val.ToString(), out n))
            {
                float val1 = CbmUtils.ParseFloat(val.ToString(), 0);
                return string.Format(strFormat.ToString(), val1 / 100);
            }
            else
            {
                return string.Format(strFormat.ToString(), 0 / 100);
            }
        }
        public static string formatNum(string val, int dec)
        {
            float n = 0;
            StringBuilder strFormat = new StringBuilder();
            strFormat.Append(@"{0:0");
            if (dec > 0)
            {
                strFormat.Append(".");
                for (int i = 0; i < dec; i++)
                {
                    strFormat.Append("0");
                }
            }
            strFormat.Append("}");

            if (CbmUtils.NotEmpty(val))
            {
                float val1 = CbmUtils.ParseFloat(val, 0);
                return string.Format(strFormat.ToString(), val1);
            }
            else
            {
                return string.Format(strFormat.ToString(), 0);
            }
        }
        */

        public static string formatNum(string val)
        {
            string strRet = string.Empty;
            if (CbmUtils.NotEmpty(val))
            {
                strRet = string.Format("{0:F0}", CbmUtils.ParseDecimal(val, 0));
            }
            return strRet;
        }
        public static string formatPct(string val)
        {
            string strRet = string.Empty;
            if (CbmUtils.NotEmpty(val))
            {
                strRet = string.Format("{0:p}", CbmUtils.ParseDecimal(val.ToString(), 0) / 100);
            }
            return strRet;
        }
        public static string formatPct2(string val)
        {
            string strRet = string.Empty;
            if (CbmUtils.NotEmpty(val))
            {
                strRet = val + "%";
            }
            return strRet;
        }
        public static string formatPct(float val)
        {
            string strRet = string.Empty;
            if (CbmUtils.NotEmpty(val))
            {
                strRet = string.Format("{0:p}", CbmUtils.ParseDecimal(val.ToString(), 0) / 100);
            }
            return strRet;
        }
        public static string formatCur(string val, int dec)
        {
            return "";
        }

        public static string formatPhone(string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;

            if (s.Length < 10)
                return s;

            return "(" + s.Substring(0, 3) + ")" + s.Substring(3, 3) + "-" + s.Substring(6, 4);
        }
        public static string formatShortDate(string val)
        {
            string strRet = string.Empty;
            if (CbmUtils.NotEmpty(val))
            {
                //strRet = string.Format(CbmUtils.ParseDateTime(val.ToString(), "yyyy-MM-dd").ToShortDateString());
                strRet = CbmUtils.ParseDateTime(val.ToString()).ToShortDateString();
            }
            return strRet;
        }
        public static string getPhoneNumber(string str)
        {
            string strRet = string.Empty;
            if (CbmUtils.NotEmpty(str))
            {
                strRet = str.Replace("(", "").Replace(")", "").Replace("-", "").Replace(".", "").Replace(" ", "").Trim();
            }
            return strRet;
        }


        public static float ZeroIfNull(string str)
        {
            if (CbmUtils.NotEmpty(str))
                return 0;
            else
            {
                float n = 0;
                if (float.TryParse(str, out n))
                    return CbmUtils.ParseFloat(str, 0);
                else
                    return 0;
            }
        }

        public static string GetMonthName(int iMonth)
        {
            string[] MonthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
            if (iMonth < 13 && iMonth > 0)
                return MonthName[iMonth - 1];
            else
                return "False";
        }

        public static string Iif(string condition, string strIfTrue, string strIfFalse)
        {
            if (CbmUtils.NotEmpty(condition))
            {
                return strIfTrue;
            }
            else
            {
                return strIfFalse;
            }
        }

        public static Dictionary<string, string> getRequestStrings(System.Collections.Specialized.NameValueCollection request)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            string[] keys = request.AllKeys;
            string values = string.Empty;

            if (request != null)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    if (request[keys[i]] != null)
                    {
                        string[] subValues = request.GetValues(keys[i]);
                        if (subValues.Length > 1)
                        {
                            for (int j = 0; j < subValues.Length; j++)
                            {
                                values += subValues[j] + ", ";
                            }
                            values = values.Substring(0, subValues.Length - 1);
                        }
                        else
                        {
                            values = subValues[0].ToString();
                        }
                    }
                    else
                    {
                        values = string.Empty;
                    }
                    if (keys[i] != null) // Only add to dictionary if the keys is not null!
                        dictionary.Add(keys[i], values);
                }
            }
            return (request == null) ? null : dictionary;
        }
        public static string getRequestValue(string key, Dictionary<string, string> dictionary)
        {
            string returnValue = string.Empty;
            if (dictionary.ContainsKey(key))
            {
                returnValue = dictionary[key].ToString();
            }
            else
            {
                returnValue = string.Empty;
            }
            return returnValue;
        }


        #endregion
    }

}